<template>
  <div class="page-dialog">
    <el-dialog title="信息采集" :visible.sync="showDialog" width="30%" :before-close="_handleClose">
      <el-steps :active="step-1" align-center finish-status="success">
        <el-step title="表单1"></el-step>
        <el-step title="表单2"></el-step>
        <el-step title="表单3"></el-step>
        <el-step title="表单4"></el-step>
      </el-steps>
      <div class="formContainer">
        <el-form :model="ruleForm" ref="ruleForm" label-width="100px" class="demo-ruleForm">
          <div v-for="item in maxStep" :key="item+'formitem'">
            <div v-if="step === item">
              <el-form-item
                :key="item+'name'"
                :label="'活动名称'+item"
                :prop="'name'+item"
                :rules="rules"
              >
                <el-input v-model="ruleForm['name'+item]"></el-input>
              </el-form-item>
              <el-form-item
                :key="item+'region'"
                :label="'活动区域'+item"
                :prop="'region'+item"
                :rules="rules"
              >
                <el-select v-model="ruleForm['region'+item]" placeholder="请选择活动区域">
                  <el-option label="区域一" value="shanghai"></el-option>
                  <el-option label="区域二" value="beijing"></el-option>
                </el-select>
              </el-form-item>
              <el-form-item
                :key="item+'date'"
                :label="'活动时间'+item"
                :rules="rules"
                :prop="'date'+item+1"
              >
                <el-col :span="11">
                  <el-form-item>
                    <el-date-picker
                      type="date"
                      placeholder="选择日期"
                      v-model="ruleForm['date'+item+1]"
                      style="width: 100%;"
                    ></el-date-picker>
                  </el-form-item>
                </el-col>
                <el-col class="line" :span="2">-</el-col>
                <el-col :span="11">
                  <el-form-item>
                    <el-time-picker
                      placeholder="选择时间"
                      v-model="ruleForm['date'+item+2]"
                      style="width: 100%;"
                    ></el-time-picker>
                  </el-form-item>
                </el-col>
              </el-form-item>
              <el-form-item :key="item+'delivery'" :label="'即时配送'+item">
                <el-switch v-model="ruleForm['delivery'+item]"></el-switch>
              </el-form-item>
              <el-form-item
                :key="item+'type'"
                :label="'活动性质'+item"
                :rules="rules"
                :prop="'type'+item"
              >
                <el-checkbox-group v-model="ruleForm['type'+item]">
                  <el-checkbox label="美食/餐厅线上活动" name="type"></el-checkbox>
                  <el-checkbox label="地推活动" name="type"></el-checkbox>
                  <el-checkbox label="线下主题活动" name="type"></el-checkbox>
                  <el-checkbox label="单纯品牌曝光" name="type"></el-checkbox>
                </el-checkbox-group>
              </el-form-item>
              <el-form-item
                :key="item+'resource'"
                :label="'特殊资源'+item"
                :rules="rules"
                :prop="'resource'+item"
              >
                <el-radio-group v-model="ruleForm['resource'+item]">
                  <el-radio label="线上品牌商赞助"></el-radio>
                  <el-radio label="线下场地免费"></el-radio>
                </el-radio-group>
              </el-form-item>
              <el-form-item
                :key="item+'desc'"
                :label="'活动形式'+item"
                :rules="rules"
                :prop="'desc'+item"
              >
                <el-input type="textarea" v-model="ruleForm['desc'+item]"></el-input>
              </el-form-item>
            </div>
          </div>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer btns">
        <el-button type="primary" @click="_nextStep">{{ btnName }}</el-button>
        <el-button type="primary" v-if="step!==1" @click="_preStep">{{ '上一步' }}</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  model: {
    prop: "showDialog",
    event: "close",
  },
  data() {
    return {
      step: 1,
      maxStep: 4,
      btnName: "",
    };
  },
  props: {
    showDialog: {
      type: Boolean,
      default: false,
    },
    ruleForm: {
      type: Object,
      default: () => {
        return {
          name1: "",
          name2: "",
          name3: "",
          name4: "",
          region1: "",
          region2: "",
          region3: "",
          region4: "",
          date11: "",
          date12: "",
          date21: "",
          date22: "",
          date31: "",
          date32: "",
          date41: "",
          date42: "",
          delivery1: false,
          delivery2: false,
          delivery3: false,
          delivery4: false,
          type1: [],
          type2: [],
          type3: [],
          type4: [],
          resource1: "",
          resource2: "",
          resource3: "",
          resource4: "",
          desc1: "",
          desc2: "",
          desc3: "",
          desc4: "",
        };
      },
    },
  },
  computed: {
    rules() {
      return [{ required: true, message: "请确认必填项", trigger: "blur" }];
    },
  },
  watch: {
    step: {
      handler(val) {
        if (val === this.maxStep) {
          this.btnName = "提交";
        } else {
          this.btnName = "继续下一步";
        }
      },
      immediate: true,
    },
  },
  methods: {
    _handleClose() {
      this.$emit("close", false);
    },
    _preStep() {
      if (this.step === 1) {
        return;
      } else {
        this.step -= 1;
      }
    },
    _nextStep() {
      this.$refs["ruleForm"].validate((valid) => {
        if (valid) {
          if (this.btnName === "提交") {
            this.$emit("submit", this.ruleForm);
          }
          if (this.step === this.maxStep) {
            this.btnName = "提交";
          } else {
            this.step += 1;
          }
        } else {
          return false;
        }
      });
    },
    submitForm() {
      this.$refs["ruleForm"].validate((valid) => {
        if (valid) {
          alert("submit!");
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
};
</script>

<style scoped lang='less'>
.page-dialog {
  user-select: none;
}
/deep/.el-dialog__body {
  padding: 0px 30px 20px 20px;
}
.btns {
  display: flex;
  justify-content: space-between;
  flex-direction: row-reverse;
}
.el-steps {
  width: 200%;
  transform: scale(.5) translateX(-50%) scale(1.2);
  margin-bottom: 10px;
}
</style>
